#!/usr/bin/env python

class Visitor1:
  def maxFunc(self):
    '''Executes for maximum counter's values.'''
    print 'Maximum function Visitor1'
    return 1
  def minFunc(self):
    '''Executes for minimum counter's values.'''
    print 'Minimum function Visitor1'
    return 1
  def normFunc(self):
    '''Executes for normal counter's values.'''
    print 'Normal function Visitor1'
    return 1

class Visitor2:
  def maxFunc(self):
    print 'Maximum function Visitor2'
    return 1
  def minFunc(self):
    print 'Minimum function Visitor2'
    return 1
  def normFunc(self):
    print 'Normal function Visitor2'
    return 1
    
if __name__ == "__main__":
  import sys
  import subprocess
  import time
  import pyrfcon
  import perfcontrol

  '''Start new process'''
  process = subprocess.Popen(sys.argv[1:4])
  pid = process.pid

  '''
  Attach to already running process (for example to process with PID = 228)
  pid = 228
  '''
  counter = 'cpuUsage' # name of controlled counter
  max = 40 #maximum counter's value
  min = 30 #minimum counter's value
  insensitiveRange = 2 #insensitive range of counter's values
  
  '''
  To start to monitor process and its threads, you must first create MonitorThread object.
  By default MonitorThread saves the process info every 0.5 seconds, but you can set necessary updateInterval (in seconds). 
  '''
  updateInterval = 1
  monitorThread = pyrfcon.MonitorThread(pid, updateInterval)
  
  '''
  To start to control counter's performance, you must first create PerformanceControl object.
  By default max = 80, min = 10, insensitiveRange = 4.
  '''
  
  perfControl = perfcontrol.PerformanceControl(monitorThread, counter, max, min, insensitiveRange)
  
  '''To create Visitor's object.'''
  visit1 = Visitor1()
  visit2 = Visitor2()
  
  '''To start to control performance.'''
  perfControl.start()
  '''To set visitor's state functions to perfControl state functions (maxFunc, minFunc and normFunc).'''
  perfControl.accept(visit1)
  '''To set limits for calls of state functions (maxFunc, minFunc or normFunc) and to wait for reach one of limits.'''
  
  print perfControl.waitReachLimit(4,4,16)
  
  perfControl.accept(visit2)
  print perfControl.waitReachLimit(3,3,16)
  
  perfControl.stop()


